Skip to content

chore: stop unit test OOM from a spinning synchronizer loop - #390

Merged
abelonogov-ld merged 3 commits into
mainfrom
andrey/fix-unit-test-heap
Aug 18, 2026
Merged

chore: stop unit test OOM from a spinning synchronizer loop#390
abelonogov-ld merged 3 commits into
mainfrom
andrey/fix-unit-test-heap

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

What happened

FDv2DataSourceTest.recoveryResetsToFirstAvailableSynchronizer intermittently fails CI with OutOfMemoryError, reported against tearDown's executor.shutdownNow() and Arrays.copyOf. It is unrelated to whatever PR happens to be running; it is a pre-existing flake in this test.

Root cause

The test's synchronizer factories return the same MockQueuedSynchronizer instance on every build() call, while the real factories construct a new one. SourceManager.getNextAvailableSynchronizerAndSetActive() closes the synchronizer it is switching away from, and MockQueuedSynchronizer.next() on a closed instance returns an already-completed SHUTDOWN.

So once recovery has cycled through both slots, every instance is closed and the inner loop in runSynchronizers exits immediately, while the outer loop is round-robin (getNextAvailableSynchronizer wraps its index) and re-enters with no delay. Each pass logs Synchronizer '{}' is starting. into LogCaptureRule's LogCapture, which retains every message for the life of the test, so the live heap grows until the JVM is exhausted.

Instrumenting the factories shows the scale of the spin. In a single ~10 second run of this one test, each factory was invoked about 630,000 times, where the test intends roughly two:

PROBE executorQueue=0 firstCalls=629883 secondCalls=629883

And live heap after each full GC climbs monotonically, never recovering:

Pause Full 2047M->1808M(2048M)
Pause Full 2041M->1912M(2048M)
...
Pause Full 2038M->2026M(2048M)

The fix

Build a fresh synchronizer per factory call, as production does. A new instance is never pre-closed, so next() serves its queued results and then returns a future that only completes later, which restores the pacing the test intended: the cycle waits on the 1s fallback and 2s recovery timers instead of spinning. Live heap after full GC then stays flat at ~75M.

Why the stack trace points at shutdownNow()

That is where the OOM surfaces, not where the memory went. shutdownNow() calls drainQueue(), whose toArray needs a fresh allocation (Arrays.copyOf), and on an already-exhausted heap that is simply the allocation that happens to fail. As the probe above shows, the executor's queue is empty (executorQueue=0): during the spin every result is SHUTDOWN, so the fallback timer never arms, and the recovery timer is not built once the index has been reset to the prime synchronizer. The retained memory is the accumulated log capture, not scheduled tasks or pending futures.

Test plan

  • ./gradlew test passes locally
  • Verified the live heap after full GC stays flat (~75M) with the fix, instead of climbing to 2026M of 2048M
  • Verified at the current 512m default test heap, so the fix does not depend on any heap change

Nothing shipped changes, so this is chore and should not cut a release.

Related

Raising the forked test JVM's heap is split out into its own PR as headroom. It does not fix this OOM (the retention fills any heap — it still OOMs at 2g), so this PR stands alone.

Follow-up worth considering (not in this PR)

The hot loop is reachable in principle outside tests: if every synchronizer reports SHUTDOWN immediately, runSynchronizers spins round-robin with no backoff, logging each pass. On a device that means CPU burn and log spam. Production factories build fresh synchronizers that block on I/O, so this is unlikely in practice, but a guard or backoff in that loop may be worth adding by whoever owns FDv2.

recoveryResetsToFirstAvailableSynchronizer handed the same mock synchronizer
back from its factory on every build, but SourceManager closes the synchronizer
it switches away from, and a closed one reports SHUTDOWN immediately. Once both
mocks were closed the round-robin loop in runSynchronizers re-entered with no
delay, logging on each pass into a LogCapture that retains every message, so the
live heap climbed until it exhausted the test JVM. Building a fresh synchronizer
per call, as the real factories do, keeps the live set flat.

Also size the forked test JVM, which Gradle otherwise leaves at 512m
(org.gradle.jvmargs only sizes the daemon). The suite ran against that ceiling,
and the extra headroom cuts full GCs during the run from ~1050 to ~240.

Co-authored-by: Cursor <cursoragent@cursor.com>
@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner August 17, 2026 20:56
abelonogov-ld and others added 2 commits August 17, 2026 14:08
Keeps this branch dedicated to the unit test memory problem. The
new Integer/new Long warnings in FlagTest are unrelated noise and
belong on their own branch.

Co-authored-by: Cursor <cursoragent@cursor.com>
Leaves this branch as only the fix for the spinning synchronizer loop.
The heap size is headroom, not the fix, and is easier to review alone.

Co-authored-by: Cursor <cursoragent@cursor.com>
@abelonogov-ld
abelonogov-ld merged commit 347ca2c into main Aug 18, 2026
7 of 8 checks passed
@abelonogov-ld
abelonogov-ld deleted the andrey/fix-unit-test-heap branch August 18, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants